package com.et114.modules.org.action;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.components.acegi.Constants;
import com.et114.components.tree.tree51.BaseTree51Model;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.core.exception.CheckException;
import com.et114.core.utility.GetterUtil;
import com.et114.core.utility.StringPool;
import com.et114.modules.org.form.GroupForm;
import com.et114.modules.org.message.FuncMessageManager;
import com.et114.modules.org.message.OrgMessageManager;
import com.et114.modules.org.services.GroupManager;
import com.et114.modules.org.services.OrgFactory;
import com.et114.modules.org.services.RoleManager;
import com.et114.modules.org.utility.SessionUserInfo;
import com.et114.modules.org.utility.tree.OrgGroupTree;
import com.et114.modules.org.vo.GroupRelVo;
import com.et114.modules.org.vo.GroupVo;
import com.et114.modules.org.vo.RoleVo;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

//@author  ZHOUXY , guanhw 
public class GroupAction extends BaseDispatchAction {
	public final static String grpCheckStr = "_fw_grpCehck" ; 
	public ActionForward loadPartyTree ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		SessionUserInfo currentUser = getCurrentUser ( request );
		HttpSession session = request.getSession ( ) ; 
		GroupForm groupform = ( GroupForm ) form;
		GroupManager fm = OrgFactory.getGroupManager ( );
		//
		Long grpId = groupform.getGrpId ( );
		List els = new ArrayList();
		String partyTree;
		// session
		groupform.setCrtUser ( currentUser.getUserId ( ) );
		//
		if ( groupform.isValidGrpId ( ) ) {
			els = fm.getChildGroup (  grpId  );
			partyTree = generateGroupTreeStr ( fm , true , els , null , BaseTree51Model.IS_TARGET_N , request.getLocale ( ) );
		} else {
			byte currentUserMaxRoleType = currentUser.getCurrentUserMaxRoleType ( ) ;
			if ( currentUserMaxRoleType < Constants.ROLE_TYPE_ORG_MANAGER ) {
				els = /*fm.getChildGroup ( new Long ( Constants.ORG_GROUP_BASE_VALUE ) ) ;//*/fm.getRootGroup ( );
			}
			else if ( currentUserMaxRoleType == Constants.ROLE_TYPE_ORG_MANAGER ) { 
				session.removeAttribute ( grpCheckStr ) ;
				List roles = OrgFactory.getUserManager ( ).getAllRolesByUserParams  (
						Constants.ROLE_TYPE_ORG_MANAGER ,
						currentUser.getUserId ( ) );
			
				if ( roles.size ( ) > 0 ) {
					StringBuffer roleIds = new StringBuffer() ;
					for ( int i = 0 ; i < roles.size ( ) ; i ++ ) {
						RoleVo role = ( RoleVo ) roles.get ( i ) ;
						roleIds.append ( role.getRoleId ( ).toString ( ) )  ;
						if ( ( i + 1 ) != roles.size ( ) ) roleIds.append ( StringPool.COMMA )  ;
					}
	
					if ( roleIds.length ( ) > 0 ) {
						Map groupMap = OrgFactory.getRoleManager ( ).getGroupsAnByRoles ( roleIds.toString ( ) ) ;
						//cache me
						Map tmp = new HashMap ( ) ; 
						tmp.putAll ( groupMap ) ; 
						session.setAttribute ( grpCheckStr , tmp ) ;
						//ge
						els =  OrgFactory.getRoleManager ( ).getRootsGroupsByRolesSorts( groupMap )  ; 						
						
						if ( els.size ( ) > 0  ) {
						}
						else {
						}
					}
				} 
			}
			else {
				els = /*fm.getChildGroup ( new Long ( Constants.ORG_GROUP_BASE_VALUE ) ) ;*/ fm.getRootGroup ( );
			}
			
			String cugid = null ; 
			String ist = BaseTree51Model.IS_TARGET_Y ; 
			if ( session.getAttribute ( BaseTree51Model.LOC_VALUES ) != null ) {
				cugid = session.getAttribute ( BaseTree51Model.LOC_VALUES ).toString ( ) ;
				ist = BaseTree51Model.IS_TARGET_N ; 
			}
			else if ( els.size ( ) > 0  ) 
				cugid = ( ( GroupVo ) els.get ( 0 ) ).getGrpId ( ).toString ( ) ;
			
			
			partyTree = generateGroupTreeStr ( fm ,  false , els , cugid , ist ,  request.getLocale ( )  );
		}
		request.setAttribute ( "defaultTabName" , FuncMessageManager.get ( request.getLocale ( )  ).getMessage ( "group.tree.root" )  ) ; 
		request.setAttribute ( "partyTree" , partyTree );
		return mapping.findForward ( "partyTree" );
	}
	  

	
	private String generateGroupTreeStr ( GroupManager gm ,  boolean isLeaf , List list ,
			String locValue , String isTarget , java.util.Locale locale )
			throws CheckException {
    	OrgGroupTree orgGroupTree = new OrgGroupTree( ) ; 
    	orgGroupTree.setBaseName ( FuncMessageManager.get ( locale ).getMessage ( "group.tree.root" ) ) ;
    	orgGroupTree.setSrc ( "groupAction.go?method=loadPartyTree&" ) ; 
    	orgGroupTree.setHref ( "groupAction.go?method=toGroupTabs&" ) ;
    	orgGroupTree.setRoot ( !isLeaf ) ;
    	orgGroupTree.setTarget ( "frmRight" ) ; 
    	orgGroupTree.setIsTarget ( isTarget ) ; 
    	orgGroupTree.setChildNodes (  list ) ;
    	if ( locValue != null )
    		orgGroupTree.setLocValue ( locValue ) ; 
    	orgGroupTree.setOnclick ( "initTree51Node( this )" ) ; 
    	return gm.generateGroupTreeStr( orgGroupTree ) ;
	}
	
	
	
	
	public ActionForward toGroupTabs ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		String grpId = request.getParameter ( "grpId" ) == null ? String.valueOf ( Constants.ORG_GROUP_BASE_VALUE ) : request
				.getParameter ( "grpId" );
		request.setAttribute ( "grpId" , grpId );
		return mapping.findForward ( "toGroupTabs" );
	}
	
	//TODO  递归太tmd多了 
	public ActionForward toGroupIndex ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		Long grpId = groupForm.getGrpId ( );
		StringBuffer navs = new StringBuffer ( );
		StringBuffer lova = new StringBuffer ( ) ;
		if ( groupForm.isValidGrpId ( ) ) {
			GroupRelVo groupRel = getGroupManager ( ).loadGroupRel ( grpId ) ; 
			String paths = GetterUtil.get ( groupRel.getGrpPath ( ) , null ) ; 
			if ( paths != null && paths .indexOf ( StringPool.SLE ) > 0 ) {
				String[] seps = paths.split ( StringPool.SLE ) ;
				List groups  = getGroupManager ( ).getGroupsByIds ( seps ) ; 
				for ( int i = 0 ; i < groups .size ( ) ; i ++ ) {
					GroupVo __dt = ( GroupVo ) groups.get ( i ) ;
					if ( __dt != null ) {
						navs.append ( __dt.getChnName ( ) ) ; 
						lova.append ( __dt.getGrpId ( ) ) ; 
						if ( groups.size() - 1 != i ) {
							navs.append ( StringPool.FORWARD_SLASH ) ;
							lova.append ( StringPool.COMMA ) ; 
						}						
					}					
				}
			}
			else {
				GroupVo __dt = getGroupManager ( ).loadGroup ( Long.valueOf ( paths ) ) ; 
				navs.append ( __dt.getChnName ( ) ) ;
				lova.append ( __dt.getGrpId ( ) ) ;
			}
			request.getSession ( ).setAttribute ( "navs" , navs );
			request.getSession ( ).setAttribute ( BaseTree51Model.LOC_VALUES , lova );
		}
		return mapping.findForward ( "toGroupIndex" );
	}
    
    
	
	public ActionForward listGroup ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		HttpSession session = request.getSession ( ) ; 
		
		GroupForm groupForm = ( GroupForm ) form;
		GroupVo groupVo = new GroupVo ( );
		BeanUtils.copyProperties ( groupVo , groupForm );
		SessionUserInfo currentUser = getCurrentUser ( request );
		byte currentUserMaxRoleType = currentUser.getCurrentUserMaxRoleType ( ) ;
		if ( currentUserMaxRoleType == Constants.ROLE_TYPE_ORG_MANAGER ) {  
			if (  session.getAttribute ( grpCheckStr ) != null && 
					!( ( Map ) session.getAttribute ( grpCheckStr ) ).containsKey ( groupVo.getGrpId ( ) )
					/*!OrgFactory.getGroupManager ( ).checkGrpManager( currentUser.getUserId ( ) , groupVo.getGrpId ( ) ) */ )   {
				error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.group.manager.noauth" , request.getLocale ( ) ) );
				request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
				return mapping.findForward( "grouperror" ) ;
			}
		}
		
		List groupList = getGroupManager ( ).queryChildGroupNodes ( groupVo );
		groupForm.setGroupList ( groupList );
		request.setAttribute ( "elssize" , new Integer( groupList.size ( ) ) ) ;
		request.setAttribute ( Constants.CURRENT_URSER_MAX_ROLETYPE , new Integer( currentUserMaxRoleType ) ) ;				
		return mapping.findForward ( "groupList" );
	}
	
	//to order
	public ActionForward toOrderGroups ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		GroupVo groupVo = new GroupVo ( );
		BeanUtils.copyProperties ( groupVo , groupForm );
		Long grpId = groupVo.getGrpId ( ) ; 
		if ( grpId != null && grpId.longValue ( ) != 0L ) {
			List groupList = getGroupManager ( ).queryChildGroupNodes ( groupVo );
			request.setAttribute ( "groups" , groupList ) ;			
		} 
		return mapping.findForward ( "toOrder" ) ;
	}
	
	//TODO 改为数据库端循环更新
	public ActionForward orderGroups ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		Long grpId = groupForm.getGrpId ( ) ; 
		if ( grpId != null && grpId.longValue ( ) != 0L ) {
			long grpNo = grpId.longValue ( ) * OrgFactory.getGroupManager ( ).getmSpil ( ) ; 
			String orders = groupForm.getOrders ( ) ; 
			if ( orders != null && !"".equals ( orders ) ) {
				StringTokenizer st = new StringTokenizer( orders , StringPool.SEMICOLON ) ;
				int count = st.countTokens ( ) ; 
				for (  int i = 0 ; i <  count ; i ++ ) {
					Long ogrpId = Long.valueOf ( st.nextToken() );
					GroupVo group = getGroupManager ( ).loadGroup ( ogrpId ) ;
					group.setGrpNo ( new Long( grpNo ) ) ; 
					grpNo ++ ;
					getGroupManager ( ).updateGroup ( group ) ;
				}
			}			
		}
		return new ActionForward( "groupAction.go?method=toOrderGroups&m=0&grpId=" + grpId , true ) ; 
	}
	
	
	public ActionForward editGroup ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		HttpSession session = request.getSession ( ) ; 
		SessionUserInfo currentUser = getCurrentUser ( request );
		byte currentUserMaxRoleType =   currentUser.getCurrentUserMaxRoleType ( ) ; 
		GroupForm groupForm = ( GroupForm ) form;
		Long grpId = groupForm.getGrpId ( ) ; 
		if ( groupForm.isValidGrpId ( ) ) {
			//valid
			if ( currentUserMaxRoleType == Constants.ROLE_TYPE_ORG_MANAGER ) {  
				if (  session.getAttribute ( grpCheckStr ) != null && 
						!( ( Map ) session.getAttribute ( grpCheckStr ) ).containsKey ( grpId ) )	 {			
				//if ( !OrgFactory.getGroupManager ( ).checkGrpManager( currentUser.getUserId ( ) , grpId ) )   {
					error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.group.manager.noauth" , request.getLocale ( ) ) );
					request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
					return mapping.findForward( "grouperror" ) ;
				}
			}
			
			
			GroupVo groupVo = OrgFactory.getGroupManager ( ).loadGroup ( grpId );
			GroupRelVo groupRelVo = OrgFactory.getGroupManager ( ).loadGroupRel ( grpId ) ; 
			
			BeanUtils.copyProperties ( groupForm , groupVo );			
			BeanUtils.copyProperties ( groupForm , groupRelVo );

			//check entity
			GroupRelVo _groupRelVo = OrgFactory.getGroupManager ( ).loadGroupRel ( groupRelVo.getFgrpId ( ) ) ; 
			if( ( groupRelVo != null && Constants.ORG_GROUP_ENTITY_Y.equals ( groupRelVo.getGrpFlag ( ) ) ) 
					|| ( _groupRelVo != null && Constants.ORG_GROUP_ENTITY_Y.equals ( _groupRelVo.getGrpFlag ( ) ) ) || groupRelVo.getFgrpId ( ).longValue() == 0  ) {
				request.setAttribute ( "ORG_GROUP_ENTITY_HAS" , "YES" ) ; 
		        List groupEntitys = DictionaryCache.getInstance ( ).listBizOptions ( Constants.ORG_GROUP_ENTITY , false );
				request.setAttribute ( "groupEntitys" , groupEntitys ) ;								
			}
			
			request.setAttribute ( "cfgrpid" , groupRelVo.getFgrpId ( ) );
		}
		else {
			//check entity
			if ( groupForm.getFgrpId ( ) != null &&  groupForm.getFgrpId ( ).longValue ( ) != 0 ) {
				GroupRelVo _groupRelVo = OrgFactory.getGroupManager ( ).loadGroupRel ( groupForm.getFgrpId ( ) ) ; 
				if( _groupRelVo != null && Constants.ORG_GROUP_ENTITY_Y.equals ( _groupRelVo.getGrpFlag ( ) ) ) {
					request.setAttribute ( "ORG_GROUP_ENTITY_HAS" , "YES" ) ; 
				}				
			}
			if ( groupForm.getFgrpId ( ).longValue ( ) == 0  ) request.setAttribute ( "ORG_GROUP_ENTITY_HAS" , "YES" ) ; 
			
			List groupEntitys = DictionaryCache.getInstance ( ).listBizOptions ( Constants.ORG_GROUP_ENTITY , false );
			request.setAttribute ( "groupEntitys" , groupEntitys ) ;
		}
		request.setAttribute ( Constants.CURRENT_URSER_MAX_ROLETYPE , new Integer( currentUserMaxRoleType ) ) ;				
		request.setAttribute ( "msg" , request.getParameter ( "msg" ) );
		return mapping.findForward ( "groupEdit" );
	}

	
	
	
	public ActionForward saveGroup ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		HttpSession session = request.getSession ( ) ; 
		GroupForm groupForm = ( GroupForm ) form;
        SessionUserInfo currentUser = getCurrentUser ( request );
		byte currentUserMaxRoleType =  currentUser.getUserRoleType ( ) ; 
        if ( currentUserMaxRoleType > Constants.ROLE_TYPE_ORG_MANAGER ) 
        	throw new Exception( Constants.AUTH_ACCESSDENIED ) ;   
		GroupVo groupVo = new GroupVo ( );
		BeanUtils.copyProperties ( groupVo , groupForm );
		groupVo.setOperUser ( currentUser.getUserId ( ) );
		GroupRelVo groupRelVo = new GroupRelVo ( );
		BeanUtils.copyProperties ( groupRelVo , groupForm );
		String cfgrpid = request.getParameter ( "cfgrpid" ) == null ? null : request.getParameter ( "cfgrpid" );
		groupRelVo.setOperUser ( currentUser.getUserId ( ) );
		
		//check code 
		if ( groupRelVo.getGrpCode ( ) != null 
				&& !"".equals ( groupRelVo.getGrpCode ( ).trim ( ) ) 
				&& !groupRelVo.getGrpCode ( ).equals ( groupForm.getOldGrpCode ( ) )	)	 {
			GroupRelVo groupRelVo__ = OrgFactory.getGroupManager ( ).loadGroupRelByCode ( groupRelVo.getGrpCode ( ) );
			if ( groupRelVo__ != null ) {
				error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.group.code.exist" , request.getLocale ( ) ) );
				request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
				return mapping.findForward( "grouperror" ) ;				
			}
		}
		
		if ( groupForm.isValidGrpId ( ) ) { //update
			//valid
			if ( currentUserMaxRoleType == Constants.ROLE_TYPE_ORG_MANAGER ) {  
				//if ( !OrgFactory.getGroupManager ( ).checkGrpManager( currentUser.getUserId ( ) , groupVo.getGrpId ( ) ) )   {
				if (  session.getAttribute ( grpCheckStr ) != null && 
						!( ( Map ) session.getAttribute ( grpCheckStr ) ).containsKey ( groupVo.getGrpId ( ) ) )	 {							
					error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.group.manager.noauth" , request.getLocale ( ) ) );
					request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
					return mapping.findForward( "grouperror" ) ;
				}
			}
			
			fillUpdtUser ( request , groupVo );
			GroupVo vo = getGroupManager ( ).loadGroup ( groupVo.getGrpId ( ) );
			if ( ! vo.getChnName ( ).equals ( groupVo.getChnName ( ) ) ) {
				if ( ( getGroupManager ( ).countGroups ( groupVo ) ).longValue ( ) > 0 ) {
					error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.groupname.already.exist" , request.getLocale ( ) )  );
					request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
					return mapping.findForward( "glerror" ) ;
				}
			}
			groupRelVo.setFgrpId ( Long.valueOf ( cfgrpid ) );
			getGroupManager ( ).updateGroup ( groupVo , groupRelVo );
			StringBuffer to = new StringBuffer( "orgSuccess.go?type=editGroup" );
			to.append ( "&grpId=" ).append ( groupForm.getGrpId ( ) ) ;
			return new ActionForward( to.toString ( ) , true ) ;
			
		}       
		else { //new
			if ( currentUserMaxRoleType == Constants.ROLE_TYPE_ORG_MANAGER ) {  
				//if ( !OrgFactory.getGroupManager ( ).checkGrpManager( currentUser.getUserId ( ) , groupVo.getFgrpId ( ) ) )   {
				if (  session.getAttribute ( grpCheckStr ) != null && 
						!( ( Map ) session.getAttribute ( grpCheckStr ) ).containsKey ( groupVo.getFgrpId ( ) ) )	 {											
					error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.group.manager.noauth" , request.getLocale ( ) ) );
					request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
					return mapping.findForward( "grouperror" ) ;
				}
			}
			
			fillCrtUser ( request , groupVo );
			fillCrtUser ( request , groupRelVo );
			if ( ( getGroupManager ( ).countGroups ( groupVo ) ).longValue ( ) > 0 ) {
				error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.groupname.already.exist" , request.getLocale ( ) ) );
				request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
				return mapping.findForward( "glerror" ) ;
			}
			getGroupManager ( ).createGroup ( groupVo , groupRelVo );
			StringBuffer to = new StringBuffer( "orgSuccess.go?type=group" );
			to.append ( "&fgrpId=" ).append ( groupForm.getFgrpId ( ) ) ;
			return new ActionForward( to.toString ( ) , true ) ;
        }
    }
	
	public ActionForward removeGroup ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		HttpSession session = request.getSession ( ) ; 
		GroupForm groupForm = ( GroupForm ) form;
		SessionUserInfo currentUser = getCurrentUser ( request );
		byte currentUserMaxRoleType = currentUser.getCurrentUserMaxRoleType ( ) ; 
        if ( currentUserMaxRoleType > Constants.ROLE_TYPE_ORG_MANAGER ) 
        	throw new Exception( Constants.AUTH_ACCESSDENIED ) ;   
		
		//valid
		if ( currentUserMaxRoleType == Constants.ROLE_TYPE_ORG_MANAGER ) {  
			//if ( !OrgFactory.getGroupManager ( ).checkGrpManager( currentUser.getUserId ( ) , groupForm.getGrpId ( ) ) )   {
			if (  session.getAttribute ( grpCheckStr ) != null && 
					!( ( Map ) session.getAttribute ( grpCheckStr ) ).containsKey ( groupForm.getGrpId ( ) ) )	 {										
				error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.group.manager.noauth" , request.getLocale ( ) ) );
				request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
				return mapping.findForward( "grouperror" ) ;
			}
		}		
		
        getGroupManager ( ).batchRemoveGroups ( groupForm.getDeleteId( ) , currentUser.getUserId ( ) );
        getGroupManager ( ).batchRemoveGroupRel ( groupForm.getDeleteId( ) , currentUser.getUserId ( ) );
        return new ActionForward( "groupAction.go?method=listGroup&m=0&grpId=" + groupForm.getGrpId ( ) , true  ) ;
    }
	
	//group operator
	public ActionForward selectGroup ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		SessionUserInfo currentUser = getCurrentUser ( request );
		GroupVo groupVo = new GroupVo();
		StringBuffer sb = new StringBuffer();
		Long grpId = groupForm.getGrpId ( ) ;
		groupVo.setGrpId( grpId );
		List grouplist = getGroupManager ( ).getGroupTrees(groupVo);
		for ( int i = 0 ; i < grouplist.size ( ) ; i ++ ) {
			groupVo = ( GroupVo ) grouplist.get ( i );
			sb.append ( "d.add(" + groupVo.getGrpId ( ) + ","
							+ groupVo.getFgrpId ( ) + ",'" + groupVo.getChnName ( )
							+ "');" );
		}
		String msg = request.getParameter ( "msg" );
		request.setAttribute ( "msg" , msg );
		request.setAttribute ( "grouptree" , sb.toString ( ) );
		request.setAttribute ( "grpId" , grpId );
		
		request.setAttribute ( Constants.CURRENT_URSER_MAX_ROLETYPE , new Integer( currentUser.getUserRoleType ( ) ) ) ;				
		return mapping.findForward ( "groupSelect" );
    }
	
	public ActionForward moveGroup ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		SessionUserInfo currentUser = getCurrentUser ( request );
		byte currentUserMaxRoleType = currentUser.getCurrentUserMaxRoleType ( ) ; 
        if ( currentUserMaxRoleType > Constants.ROLE_TYPE_ORG_MANAGER ) 
        	throw new Exception( Constants.AUTH_ACCESSDENIED ) ;   

		GroupRelVo groupRelVo = new GroupRelVo( ) ; 
		BeanUtils.copyProperties ( groupRelVo , groupForm );

		groupRelVo.setOperUser ( currentUser.getUserId ( ) );
		int  msg = 1 ; 
		if ( ( getGroupManager ( ).countMoveGroups ( groupRelVo ) ).longValue ( ) > 0 ) {
			error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.groupname.move.already.exist" , request.getLocale ( ) ) );
			request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
			msg = 0 ; 
		}
		if( msg == 1 )  {
			getGroupManager ( ).moveGroupRel ( groupRelVo );
			GroupRelVo _groupRelVo = OrgFactory.getGroupManager ( ).loadGroupRel ( groupRelVo.getFgrpId ( ) ) ;
			if ( !Constants.ORG_GROUP_ENTITY_Y .equals ( _groupRelVo.getGrpFlag ( ) ) )
				getGroupManager ( ).updateGroupRel ( _groupRelVo );
		}
		return new ActionForward ( "/groupAction.go?method=selectGroup&grpId="
									+ groupRelVo.getFgrpId ( ) + "&msg=" + msg );
    }
	
	
	public ActionForward loadGrpAuths ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		SessionUserInfo currentUser = getCurrentUser ( request );
		Long grpId = groupForm.getGrpId ( ) ; 
		List roles = OrgFactory.getGroupManager ( ).getRolesByGrpId ( grpId ) ; 
		request.setAttribute ( "roles"  , roles ) ;
		request.setAttribute ( Constants.CURRENT_URSER_MAX_ROLETYPE , new Integer( currentUser.getUserRoleType ( ) ) ) ;						
		return mapping.findForward ( "grpRoles" ) ; 
	}
	
	public ActionForward toAuthIndex ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		BizOption bizOption = OrgFactory.getRoleManager ( ).getRoleType ( Constants.ROLE_TYPE_ORG_MANAGER )  ;
		request.setAttribute ( "option" , bizOption ) ;	
		return mapping.findForward ( "authIndex" );
	}

	
	public  ActionForward loadRoleAuths( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		SessionUserInfo currentUser = getCurrentUser ( request );
		Long grpId = groupForm.getGrpId ( ) ; 
		//load all roles
		RoleManager roleManager = OrgFactory.getRoleManager ( ) ;
		List roles = roleManager.getRoles ( Constants.ROLE_TYPE_ORG_MANAGER  ) ; 
		request.setAttribute ( "roles" , roles ) ;
		
		//load select roles
		List groupOfRoles = OrgFactory.getGroupManager ( ).getRolesByGrpId ( grpId ) ;
		request.setAttribute ( "groupOfRoles" , groupOfRoles );		
		request.setAttribute ( Constants.CURRENT_URSER_MAX_ROLETYPE , new Integer( currentUser.getUserRoleType ( ) ) ) ;				
		return mapping.findForward ( "groupAuth" );
	}
	
	public  ActionForward doRoleAuth( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SessionUserInfo currentUser = getCurrentUser ( request );
        if ( currentUser.getCurrentUserMaxRoleType ( ) >= Constants.ROLE_TYPE_ORG_MANAGER ) 
        	throw new Exception( Constants.AUTH_ACCESSDENIED ) ;   
        String roleType = request.getParameter ( "roleType" ) ; 
		if ( roleType == null || Constants.ROLE_TYPE_ORG_MANAGER != Integer.valueOf ( roleType ).byteValue ( ) ) 
			throw new Exception( Constants.AUTH_ACCESSDENIED ) ;   

		GroupForm groupForm = ( GroupForm ) form;
		Long grpId = groupForm.getGrpId ( ) ; 		
		OrgFactory.getGroupManager ( ).createGroupRoleRel ( currentUser.getUserId ( ) , grpId  , groupForm.getPrincipalIds ( ) ) ;
		StringBuffer to = new StringBuffer() ; 
		to.append ( "groupAction.go?method=loadRoleAuths&grpId=" ).append (
				grpId ).append ( "&roleType=" ).append ( roleType );
		return new ActionForward( to.toString ( ) ,  true  ) ;
	}
	
	public  ActionForward removeRoleAuth( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		GroupForm groupForm = ( GroupForm ) form;
		SessionUserInfo currentUser = getCurrentUser ( request );
        if ( currentUser.getCurrentUserMaxRoleType ( ) > Constants.ROLE_TYPE_ORG_MANAGER ) 
        	throw new Exception( Constants.AUTH_ACCESSDENIED ) ;   		
		
		Long operuser = currentUser.getUserId ( );
		Long grpId = groupForm.getGrpId ( ) ; 		
		Long[] deleteIds = groupForm.getDeleteId ( ) ; 
		if ( deleteIds != null ) {
			for ( int i = 0 ; i < deleteIds .length ; i ++  ) {
				Long deleteId = deleteIds[ i ] ;
				OrgFactory.getGroupManager ( ).removeGroupRoleRel( grpId , deleteId , operuser ) ; 
			}
		}
		StringBuffer to = new StringBuffer() ; 
		to.append ( "groupAction.go?method=loadGrpAuths&grpId=" ).append ( grpId ) ; 
		return new ActionForward( to.toString ( ) ,  true  ) ;		
	}
	
    private GroupManager getGroupManager() throws CheckException {
        return OrgFactory.getGroupManager ( ) ;
    }
}